<template>
  <div class="app-container">
    <!--查询表单-->
    <div class="search-div">
      <el-form label-width="70px" size="small">
        <el-row>
          <el-col :span="8">
            <el-form-item label="小区名称">
              <el-input
                style="width: 30%"
                v-model="searchObj.name"
                placeholder="小区名称"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="创建时间">
              <el-date-picker
                v-model="createTimes"
                type="datetimerange"
                range-separator="至"
                start-placeholder="开始时间"
                end-placeholder="结束时间"
                value-format="yyyy-MM-dd HH:mm:ss"
                style="margin-right: 10px; width: 100%"
              />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row style="display: flex">
          <el-button
            type="primary"
            icon="el-icon-search"
            size="mini"
            @click="fetchData()"
            >搜索</el-button
          >
          <el-button icon="el-icon-refresh" size="mini" @click="resetData"
            >重置</el-button
          >
        </el-row>
      </el-form>
    </div>

    <!-- 工具条 -->
    <div class="tools-div">
      <el-button type="success" icon="el-icon-plus" size="mini" @click="add"
        >添 加</el-button
      >
    </div>

    <!-- 表格 -->
    <el-table
      v-loading="listLoading"
      :data="list"
      stripe
      border
      style="width: 100%; margin-top: 10px"
    >
      <el-table-column label="序号" width="70" align="center">
        <template slot-scope="scope">
          {{ (page - 1) * limit + scope.$index + 1 }}
        </template>
      </el-table-column>

      <el-table-column prop="code" label="小区编号" />
      <el-table-column prop="name" label="小区名称" />
      <el-table-column prop="address" label="落座地址" />

      <el-table-column prop="area" label="占地面积" />
      <el-table-column prop="totalBuildings" label="总楼栋数" />
      <el-table-column prop="totalHouseholds" label="总户数" />
      <el-table-column prop="greeningRate" label="绿化面积"> </el-table-column>
      <!-- <el-table-column prop="thumbnail" label="缩略图" width="160" /> -->
      <!-- <el-table-column prop="thumbnail" label="缩略图">
        <template slot-scope="scope">
          <img :src="scope.row.thumbnail" alt="" style="height: 80px" />
        </template>
      </el-table-column> -->

      <el-table-column
        prop="thumbnail"
        label="缩略图">
       <template slot-scope="scope">
        <viewer>
         <img :src="scope.row.thumbnail" alt="" style="height:80px ;">
        </viewer>
       </template>
      </el-table-column>

      <el-table-column prop="developer" label="开发商名称" />

      <el-table-column prop="estateCompany" label="物业公司名称" />
      <el-table-column prop="createTime" label="创建时间" />
      <el-table-column prop="updateTime" label="更新时间" />

      <el-table-column label="操作" width="200" align="center">
        <template slot-scope="scope">
          <el-button
            type="primary"
            icon="el-icon-edit"
            size="mini"
            @click="edit(scope.row.id)"
            title="修改"
          />
          <el-button
            type="danger"
            icon="el-icon-delete"
            size="mini"
            @click="removeDataById(scope.row.id)"
            title="删除"
          />
        </template>
      </el-table-column>
    </el-table>

    <el-dialog title="添加/修改" :visible.sync="dialogVisible" width="40%">
      <el-form
        ref="dataForm"
        :model="sysCommunity"
        label-width="150px"
        size="small"
        style="padding-right: 40px"
      >
        <el-form-item label="小区名称">
          <el-input v-model="sysCommunity.name" />
        </el-form-item>
        <el-form-item label="坐落地址">
          <el-input v-model="sysCommunity.address" />
        </el-form-item>
        <el-form-item label="占地面积（m2）">
          <el-input v-model="sysCommunity.area" />
        </el-form-item>
        <el-form-item label="开发商名称">
          <el-input v-model="sysCommunity.developer" />
        </el-form-item>
        <el-form-item label="物业公司名称">
          <el-input v-model="sysCommunity.estateCompany" />
        </el-form-item>
        <el-form-item label="总栋数">
          <el-input v-model="sysCommunity.totalBuildings" />
        </el-form-item>
        <el-form-item label="总户数">
          <el-input v-model="sysCommunity.totalHouseholds" />
        </el-form-item>
        <el-form-item label="绿化面积(%)">
          <el-input v-model="sysCommunity.greeningRate" />
        </el-form-item>
        <el-form-item label="缩略图" prop="thumbnail">
          <el-upload class="upload-demo" name="uploadImage" action="http://localhost:8085/admin/system/upload/uploadImage"
          :limit="1"
          :on-success="handleImageSuccess"
          :before-upload="handleBeforeUpload"
          :show-file-list="false"
          >
            <el-button type="primary" size="small">点击上传</el-button>
          </el-upload>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button
          @click="dialogVisible = false"
          size="small"
          icon="el-icon-refresh-right"
          >取 消</el-button
        >
        <el-button
          type="primary"
          icon="el-icon-check"
          @click="saveOrUpdate()"
          size="small"
          >确 定</el-button
        >
      </span>
    </el-dialog>

    <!-- 分页组件 -->
    <el-pagination
      :current-page="page"
      :total="total"
      :page-size="limit"
      style="padding: 30px 0; text-align: center"
      layout="total, prev, pager, next, jumper"
      @current-change="fetchData"
    />
  </div>
</template>
   
   <script>
import api from "@/api/sysCommunity/community";
export default {
  data() {
    return {
      listLoading: true,
      list: [],
      total: 0,
      page: 1,
      limit: 5,
      createTimes: [],
      searchObj: {},
      sysCommunity: {},
      dialogVisible: false,
    };
  },
  created() {
    this.fetchData(1);
  },
  methods: {

     //文件开始上传,开始屏幕遮罩
     handleBeforeUpload() {
      this.listLoading = true;
    },
    //图片上传成功的钩子函数
    handleImageSuccess(res, file) {

      console.log(res);  // 得到的是图片地址
      console.log(file);  // 得到是一个对象  
console.log("----------------------")
      this.listLoading = false;
      if (file.response != "") {
        // this.sysCommunity.image = file.response.data;
        this.sysCommunity.thumbnail = file.response;
        console.log(this.sysCommunity.thumbnail);

console.log("----------------------")
        this.$message({
          type: "info", 
          message: "图片上传成功",
          duration: 6000,
        });
      } else {
        this.$message({
          type: "info",
          message: "图片上传失败",
          duration: 6000,
        });
      }
    },


    //得到数据
    fetchData(page = 1) {
      // console.log(this.createTimes);
      this.page = page;
      if (this.createTimes && this.createTimes.length == 2) {
        this.searchObj.createTimeBegin = this.createTimes[0];
        this.searchObj.createTimeEnd = this.createTimes[1];
        // console.log(this.searchObj.createTimeBegin);
      }
      api.getPageList(this.page, this.limit, this.searchObj).then((res) => {
        this.list = res.data.records;
        this.total = res.data.total;
        this.listLoading = false;
      });
    },

    //重置按钮
    resetData() {
      // 清空表单
      this.searchObj = {};

      //清空日期条件
      this.createTimes = [];

      // 再次刷新列表数据
      this.fetchData();
    },
    // //修改按钮
    edit(id) {
      api.getCommunityById(id).then((res) => {
        // console.log(res)
        this.sysCommunity = res.data;
        //弹出弹框
        this.dialogVisible = true;
      });
    },
    //添加按钮
    add() {
      this.sysCommunity = {};
      this.dialogVisible = true;
    },
    removeDataById(id) {
      this.$confirm("此操作将永久删除该账号, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
        // 点击确定
        api.removeById(id).then((response) => {
          // 1.给出提示
          this.$message({
            type: "success",
            message: "删除成功!",
          });
          this.listLoading = true;
          //2. 再次刷新数据列表
          this.fetchData();
        });
      });
    },
    //保存/修改
    saveOrUpdate() {
      if (this.sysCommunity.id) {
        //修改
        this.update();
      } else {
        this.addCommunity();
      }
    },

    update() {
      console.log(this.sysCommunity)
      api.updateCommunity(this.sysCommunity).then((res) => {
        // 1.给出提示
        this.$message({
          type: "success",
          message: "修改成功!",
        });
        //刷新列表
        this.fetchData();
        //关闭弹框
        this.dialogVisible = false;
      });
    },

    addCommunity() {
      console.log("添加前");
      api.addCommunity(this.sysCommunity).then((res) => {
        // 1.给出提示
        this.$message({
          type: "success",
          message: "添加成功!",
        });
        //刷新列表
        this.fetchData();
        //关闭弹框
        this.dialogVisible = false;
      });
    },
  },
};
</script>
   
   <style>
</style>